<template>
  <div class="timeline-container">
    <div class="timeline-wrapper" ref="timelineWrapper">
      <div 
        class="timeline"
        :style="{ width: timelineWidth + 'px' }"
      >
        <div 
          v-for="(item, index) in items" 
          :key="index"
          class="timeline-item"
          :style="getItemStyle(index)"
          @click="handleItemClick(item, index)"
        >
          <div class="timeline-dot" :class="{ 'active': activeIndex === index }"></div>
          <div class="timeline-content" :class="{ 'active': activeIndex === index }">
            <div class="timeline-date">{{ item.date }}</div>
            <!-- <div class="timeline-title">{{ item.title }}</div>
            <div class="timeline-desc" v-if="activeIndex === index">{{ item.description }}</div> -->
          </div>
        </div>
        <div class="timeline-line" :style="{ width: timelineWidth + 'px' }"></div>
      </div>
    </div>
    
    <!-- <div class="timeline-controls" v-if="showControls">
      <button @click="scrollTimeline(-1)" :disabled="scrollPosition >= 0">←</button>
      <button @click="scrollTimeline(1)" :disabled="scrollPosition <= minScrollPosition">→</button>
    </div> -->
  </div>
</template>

<script>
export default {
  name: 'HorizontalTimeline',
  props: {
    items: {
      type: Array,
      required: true,
    },
    itemWidth: {
      type: Number,
      default: 200
    },
    showControls: {
      type: Boolean,
      default: true
    },
    activeIndex: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      scrollPosition: 0,
      wrapperWidth: 0
    }
  },
  computed: {
    timelineWidth() {
      return this.items.length * this.itemWidth
    },
    minScrollPosition() {
      return this.wrapperWidth - this.timelineWidth
    }
  },
  mounted() {
    this.updateWrapperWidth()
    window.addEventListener('resize', this.updateWrapperWidth)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.updateWrapperWidth)
  },
  methods: {
    getItemStyle(index) {
      return {
        left: (index * this.itemWidth) + 'px',
        width: this.itemWidth + 'px'
      }
    },
    handleItemClick(item, index) {
      this.$emit('item-click', { item, index })
      this.scrollToItem(index)
    },
    scrollToItem(index) {
      const targetPosition = -index * this.itemWidth + this.wrapperWidth / 2 - this.itemWidth / 2
      this.scrollPosition = Math.max(this.minScrollPosition, Math.min(0, targetPosition))
      this.$refs.timelineWrapper.scrollLeft = -this.scrollPosition
    },
    scrollTimeline(direction) {
      const scrollAmount = this.wrapperWidth * 0.8
      this.scrollPosition += direction * scrollAmount
      
      // 限制滚动范围
      this.scrollPosition = Math.max(this.minScrollPosition, Math.min(0, this.scrollPosition))
      
      this.$refs.timelineWrapper.scrollLeft = -this.scrollPosition
    },
    updateWrapperWidth() {
      if (this.$refs.timelineWrapper) {
        this.wrapperWidth = this.$refs.timelineWrapper.clientWidth
      }
    }
  },
  watch: {
    activeIndex(newVal) {
      this.scrollToItem(newVal)
    }
  }
}
</script>

<style scoped>
.timeline-container {
  position: relative;
  overflow: hidden;
}

.timeline-wrapper {
  width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  padding: 20px 0;
}

.timeline {
  position: relative;
  height: 60px;
}

.timeline-line {
  position: absolute;
  top: 7px;
  left: 0;
  height: 2px;
  background-color: #e0e0e0;
  z-index: 1;
}

.timeline-item {
  position: absolute;
  top: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  z-index: 2;
}

.timeline-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background-color: #9e9e9e;
  border: 3px solid #f5f5f5;
  transition: all 0.3s ease;
}

.timeline-dot.active {
  background-color: #2196f3;
  transform: scale(1.2);
}

.timeline-content {
  text-align: center;
  margin-top: 10px;
  padding: 10px;
  border-radius: 4px;
  transition: all 0.3s ease;
  opacity: 0.8;
  max-width: 90%;
}

.timeline-content.active {
  opacity: 1;
  background-color: rgba(33, 150, 243, 0.1);
}

.timeline-date {
  font-size: 14px;
  color: #757575;
  margin-bottom: 5px;
}

.timeline-title {
  font-weight: bold;
  color: #424242;
}

.timeline-desc {
  font-size: 12px;
  color: #616161;
  margin-top: 5px;
}

.timeline-controls {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}

.timeline-controls button {
  background: #2196f3;
  color: white;
  border: none;
  padding: 5px 15px;
  margin: 0 5px;
  border-radius: 4px;
  cursor: pointer;
}

.timeline-controls button:disabled {
  background: #bdbdbd;
  cursor: not-allowed;
}

/* 隐藏原生滚动条 */
.timeline-wrapper::-webkit-scrollbar {
  display: none;
}
</style>